﻿namespace FFF.Test.Application.DomainServices.CF
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Abp.Domain.Services;
    using Abp.Domain.Uow;
    using Abp.EntityFrameworkCore.Uow;
    using Abp.EntityFrameworkCore;
    using AutoMapper;
    using Abp.UI;
    using Ydm.Helper;
    using Ydm.Extensions;
    using Microsoft.EntityFrameworkCore;
    using AutoMapper.QueryableExtensions;
    using FFF.Test.DataAccess.Framework;
    using FFF.Test.Application.Repositories.CF;
    using FFF.Test.Entity.CF;
    using FFF.Test.Entity.CF.Dto;
    using FFF.Test.Entity.CF.View;

    /// <summary>
    /// 业务参数分组表领域层服务接口
    /// </summary>
    public class CFBizConfigGroupDomainService : YdmAbpDomainServiceBase, ICFBizConfigGroupDomainService
    {
        /// <summary>
        /// 业务参数分组表仓储
        /// </summary>
        private readonly ICFBizConfigGroupRepository _cFBizConfigGroupRepository;

        /// <summary>
        /// 业务参数仓储层
        /// </summary>
        private readonly ICFBizConfigOptionRepository _cFBizConfigOptionRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="CFBizConfigGroupDomainService"/> class.
        /// 业务参数分组表领域层操作
        /// </summary>
        /// <param name="cFBizConfigGroupRepository">业务参数分组表仓储</param>
        /// <param name="cFBizConfigOptionRepository">biz修复</param>
        public CFBizConfigGroupDomainService(
            ICFBizConfigGroupRepository cFBizConfigGroupRepository,
            ICFBizConfigOptionRepository cFBizConfigOptionRepository
        )
        {
            this._cFBizConfigGroupRepository = cFBizConfigGroupRepository;
            this._cFBizConfigOptionRepository = cFBizConfigOptionRepository;
        }

        /// <summary>
        /// 获取业务参数信息(于大明)
        /// </summary>
        /// <param name="inDto">获取业务参数信息输入dto</param>
        /// <returns>获取业务参数信息输出dto</returns>
        public async Task<GetConfigInfoOutDto> GetConfigInfoAsync(GetConfigInfoInDto inDto)
        {
            var viewGroup = this._cFBizConfigGroupRepository.GetView();
            var result = new GetConfigInfoOutDto();
            result.Dtos = await viewGroup.Where(x => x.BizGroupScopeGUID == inDto.BizOptionScopeGUID)
                .OrderBy(x => x.BizGroupOrder)
                .ThenByDescending(x => x.CreateTime)
                .Select(x => new GetConfigInfoDto { BizConfigGroupGUID = x.Id, BizConfigGroupName = x.BizConfigGroupName }).ToListAsync();
            foreach (var dto in result.Dtos)
            {
                var viewOption = this._cFBizConfigOptionRepository.GetView();
                dto.Items = await viewOption
                    .Where(x => x.BizConfigGroupGUID == dto.BizConfigGroupGUID)
                    .OrderBy(x => x.BizOptionOrder)
                    .ThenByDescending(x => x.CreateTime)
                    .ProjectTo<GetConfigInfoItem>()
                    .ToListAsync();
            }

            return result;
        }
    }
}
